<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/6/3
 * Time: 14:34
 */

namespace app\vehicle\logic;
use app\vehicle\model\Forum;
use app\vehicle\model\ForumLog;
use app\common\model\ForumCate;
class ForumLogic
{
    public function getForumCate(){
        $list = ForumCate::where('status','eq',1)->select();
        return $list;
    }
    public function getForumList($data,$user_id){
        $where[] = ['status','eq',1];
       // $data['cate_id'] && $where[] = ['cate_id','eq',$data['cate_id']];
        $time = time();
        //$where[] = ['use_time','gt',$time];
        $list = Forum::where($where)->order('create_time desc')->page($data['page'],100)->select();
        foreach($list as $k=>$v){
            $list[$k]['is_apply'] = ForumLog::where('user_id','eq',$user_id)->where('fid','eq',$v['id'])->count();
        }
        return $list;
    }
    public function getForumInfo($data,$user_id){
        Forum::where('id','eq',$data['id'])->setInc('read_num',1);
        $info = Forum::get($data['id']);
        $info['is_apply'] = ForumLog::where('user_id','eq',$user_id)->where('fid','eq',$info['id'])->count();
        return $info;
    }

    public function setForum($data,$user_id){
        $check = ForumLog::where('user_id','eq',$user_id)->where('fid','eq',$data['id'])->count();
        if($check){
            return '已报名';
        }
        $num = ForumLog::where('fid','eq',$data['id'])->count();
        $info = Forum::get($data['id']);
        if($num>=$info['number']){
            return '人数已满';
        }
        $insert['user_id'] = $user_id;
        $insert['fid'] = $data['id'];
        $insert['create_time'] = time();
        ForumLog::insert($insert);
        Forum::where('id','eq',$data['id'])->setInc('apply_number');
        return 1;
    }
}